package com.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.biz.CustomerBiz;
import com.biz.WechatMinaBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.bo.MinaLoginFormBO;
import com.entity.model.Customer;
import com.entity.threeConnect.ThirdUserInfo;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.util.LoginTicketUtil;
import com.util.SystemConstant;
import com.wechat.mina.ComponentUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * Created by asan on 2017-09-05.
 */
@RestController
@RequestMapping("/api/customer")
public class WechatController {
    //private final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private WechatMinaBiz wechatMinaBiz;

    @Autowired
    private CustomerBiz customerBiz;

    @RequestMapping(value = "/mina/wxlogin")
    public Result mimaLogin(MinaLoginFormBO model) {
        if (org.apache.commons.lang3.StringUtils.isBlank(model.getCode())) {
            return ResultGenerator.genFailResult("微信Code不能为空");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(model.getNickName())) {
            return ResultGenerator.genFailResult("用户昵称不能为空");
        }
        return ResultGenerator.genSuccessResult(wechatMinaBiz.login(model));
    }


    /**
     * 获取分享小程序二维码
     *
     * @param scene 参数
     * @param page  需生成二维码对应地址
     * @return
     */
    @RequestMapping("/mina/get_mina_qr")
    public Result getMinaQr(@RequestParam String scene, @RequestParam String page) {
        if (org.apache.commons.lang3.StringUtils.isBlank(scene)) {
            return ResultGenerator.genFailResult("请求scene参数不能为空");
        }
        if (org.apache.commons.lang3.StringUtils.isBlank(page)) {
            return ResultGenerator.genFailResult("请求page参数不能为空");
        }
        String qrUrl = null;
        try {
            qrUrl = ComponentUtil.getMinaQr("{\"scene\":\"" + scene + "\",\"page\":\"" + page + "\"}", page + scene);
        } catch (WeixinException e) {
            e.printStackTrace();
        }

        if (StringUtils.isNotBlank(qrUrl)) {
            return ResultGenerator.genSuccessResult(qrUrl);
        } else {
            return ResultGenerator.genFailResult("获取分享小程序二维码失败，请稍后重试");
        }
    }

    /*@RequestMapping("/mina/loginByPhone")
    public Result decodePhone(MinaLoginFormBO model,@RequestParam Integer customerId) {
        if(StringUtils.isBlank(model.getEncryptedData()))
        {
            return ResultGenerator.genFailResult("参数错误");
        }
        if(StringUtils.isBlank(model.getIv()))
        {
            return ResultGenerator.genFailResult("参数错误");
        }
        if (null == customerId) {
            return ResultGenerator.genFailResult("用户Id不能为空");
        }
        String code = model.getCode();
        if (org.apache.commons.lang3.StringUtils.isBlank(code)) {
            return ResultGenerator.genFailResult("微信编码不能为空");
        }
        JSONObject jsonObject=wechatMinaBiz.decodeInfo(customerId,model.getEncryptedData(),model.getIv());
        if (jsonObject == null) {
            return ResultGenerator.genFailResult(-99, "登录超时,请重试");
        }
        String phone = jsonObject.getString("purePhoneNumber");
        if (org.apache.commons.lang3.StringUtils.isBlank(phone)) {
            return ResultGenerator.genFailResult("请输入手机号");
        }
        model.setPhone(phone);
        // 更新注册用户信息
        customerBiz.updateCustomerByPhoneAndConnect(customerId, phone);
        return ResultGenerator.genSuccessResult(phone);
    }*/


    private ThirdUserInfo getThirdUserInfo(MinaLoginFormBO model) {
        ThirdUserInfo thirdUserInfo = new ThirdUserInfo();

        if (model != null) {
            thirdUserInfo.setPhone(model.getPhone());
            thirdUserInfo.setOpenId(model.getOpenid());
            thirdUserInfo.setUnionid(model.getUnionid());
            // 昵称
            thirdUserInfo.setNickname(model.getNickName());
            // 用户的性别（1是男性，2是女性，0是未知）
            thirdUserInfo.setSex(model.getGender());
            // 用户所在国家
            thirdUserInfo.setCountry(model.getCountry());
            // 用户所在省份
            thirdUserInfo.setProvince(model.getProvince());
            // 用户所在城市
            thirdUserInfo.setCity(model.getCity());
            // 用户头像
            thirdUserInfo.setHeadImgUrl(model.getAvatarUrl());
        }

        return thirdUserInfo;
    }
}
